<?php

namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\common\parser\JsonParser;
use app\common\api\ApiOperator; //引入访问api类
use yii\data\Pagination;
use yii\helpers\Url;
use app\common\helper\PdUtils;
use yii\data\ArrayDataProvider;
use app\common\helper\ArrayHelper;


class StudentQuizTestController extends Controller
{
    public $enableCsrfValidation = false;
    public function init()
    {
        parent::init();
        header('Access-Control-Allow-Origin: *');
    }
   
    public function actionQuizListBak1(){
        $error=[];
        $userId = isset($_REQUEST['userId'])&&!empty($_REQUEST['userId']) ? $_REQUEST['userId'] : $error[]='学生ID不能为空';
        if(!empty($error)){
            $result=[
                'msg'=>'缺少请求参数',
                "ver"=> "1.5.0",
                'data'=>$error,
                'code'=>'601'
            ];
            Yii::$app->end(json_encode($result));
        }else{
            $time = date("Y-m-d H:i:s");
            $resultInfo = [];
            $current_where = [':create_by' => $userId];

            $quiz_sql = "select distinct(r.id) ,qf.id quizId,qf.quiz_name,qf.begin_time,qf.result_end_time,r.status resultStatus,r.id as resultId,r.back_result_end_time,qf.quiz_status as quizStatus,cf.name courseName from t_quiz_info qf "
                . "left join t_quiz_result r on r.quiz_id=qf.id "
                . "left join t_course_info cf on cf.id=qf.t_course_id "
                . "where t_course_id in(select course_id from t_course_member where user_id=:create_by and del_flag=0) and qf.t_course_id is not null and qf.pid is null "
                . "and qf.quiz_type in('quizType.task','quizType.quiz','quizType.exam')  and qf.quiz_status='activityStatus.ongoing' and (r.status is null or r.status='' or r.status='quizResultStatus.answerIn')  and qf.del_flag=0 and r.del_flag=0 and r.create_by=:create_by and cf.del_flag=0 order by r.create_time desc ";

            $info= \Yii::$app->db->createCommand($quiz_sql)->bindValues($current_where)->queryAll();
            foreach($info as $k=>$v){
                if($v['resultStatus']=='quizResultStatus.backed'){
                    if(!empty($v['back_result_end_time']) && substr($v['back_result_end_time'], 0, 16)<$time){
                        unset($info[$k]);
                    }
                }else{
                    if(!empty($v['result_end_time']) && substr($v['result_end_time'], 0, 16)<$time){
                        unset($info[$k]);
                    }
                }
            }
            foreach($info as $key=>$val){
                $info[$key]['daibanType'] = '考试测验';
            }
            
            //实训任务
            $sql="SELECT tctr.id resultId,tct.id quizId,tct.task_title quiz_name,tct.begin_time,tctr.report_status quizStatus,
            tct.deadline begin_time,
            tct.start_time result_end_time,
            i.name AS courseName
    FROM
            t_course_task_report tctr
    LEFT JOIN t_course_task tct ON tct.id = tctr.task_id
    LEFT JOIN t_course_info i ON tct.t_course_id = i.id
    LEFT JOIN t_course_schedule s ON s.id=tctr.schedule_id
    LEFT JOIN t_base_user_profile p ON p.user_id=tct.publish_by
    WHERE
            tctr.create_by =:create_by
    AND tct.task_status IN (
            'activityStatus.ongoing',
            'activityStatus.over'
    ) and tctr.report_status='reportStatus.notComplete' 
    AND tct.del_flag = 0
    AND tctr.del_flag = 0
    AND i.del_flag=0  order by tct.publish_time desc ";

            $taskInfo = Yii::$app->db->createCommand($sql)->bindValues([':create_by'=>$userId])->queryAll();
            foreach($taskInfo as $k=>$v){
                $taskInfo[$k]['daibanType'] = '实训任务';
            }

            $resultInfo = array_merge($info,$taskInfo);
            $result=[
                'msg'=>'查询成功',
                "ver"=> "1.5.0",
                'data'=>$resultInfo,
                'code'=>'200'
            ];
            Yii::$app->end(json_encode($result));

        }
        
    }
    
    public function actionQuizList(){
        $error=[];
        $userId = isset($_REQUEST['userId'])&&!empty($_REQUEST['userId']) ? $_REQUEST['userId'] : $error[]='学生ID不能为空';
        $type = isset($_REQUEST['type'])&&!empty($_REQUEST['type']) ? $_REQUEST['type'] : 'work';
        $pageIndex = !empty($_REQUEST['pageIndex'])?$_REQUEST['pageIndex']:'1';
        $pageSize = !empty($_REQUEST['pageSize'])?$_REQUEST['pageSize']:'10';
        $keyword = isset($_REQUEST['keyword'])&&!empty($_REQUEST['keyword'])?$_REQUEST['keyword']:'';
        $limit=$pageSize*($pageIndex-1);
        if(!empty($error)){
            $result=[
                'msg'=>'缺少请求参数',
                "ver"=> "1.5.0",
                'data'=>$error,
                'code'=>'601'
            ];
            Yii::$app->end(json_encode($result));
        }else{
            $param = [':create_by' => $userId];
            $where='';
            $quiz_type = '';
            $is_quiz = true;
            switch ($type) {
                case 'work':
                    $quiz_type = 'quizType.task';
                    break;
                case 'quiz':
                    $quiz_type = 'quizType.quiz';
                    break;
                case 'exam':
                    $quiz_type = 'quizType.exam';
                    break;
                case 'task':
                    $is_quiz = false;
                    break;
                default:
                    $quiz_type = 'quizType.task';
                    break;
            }
            
            if($is_quiz){
                $param = array_merge($param,[':quiz_type'=>$quiz_type]);
                if(!empty($keyword)){
                    $param = array_merge($param,[':keyword'=>"%" . $keyword . "%"]);
                    $where = " and qf.quiz_name like :keyword ";
                }
                $sql = "select count(qf.id) from t_quiz_info qf "
                    . "left join t_quiz_result r on r.quiz_id=qf.id "
                    . "left join t_course_info cf on cf.id=qf.t_course_id "
                    . "where IF(r.STATUS='quizResultStatus.backed',r.back_result_end_time is null or r.back_result_end_time='' or SUBSTR(r.back_result_end_time,0,16)>NOW(),qf.result_end_time IS NULL OR qf.result_end_time ='' OR SUBSTR(qf.result_end_time,0,16)>NOW()) AND t_course_id in(select course_id from t_course_member where user_id=:create_by and del_flag=0) and qf.t_course_id is not null and qf.pid is null "
                    . "and qf.quiz_type = :quiz_type  and qf.quiz_status='activityStatus.ongoing' and (r.status is null or r.status='' or r.status='quizResultStatus.answerIn')  and qf.del_flag=0 and r.del_flag=0 and r.create_by=:create_by and cf.del_flag=0 $where ";
                $sum= \Yii::$app->db->createCommand($sql)->bindValues($param)->queryScalar();
                $quiz_sql = "select distinct(r.id) ,qf.id quizId,qf.quiz_type,qf.quiz_name quiz_name,qf.begin_time,qf.result_end_time,r.status resultStatus,r.id as resultId,r.back_result_end_time,qf.quiz_status as quizStatus,cf.name courseName,qf.t_course_id course_id from t_quiz_info qf "
                    . "left join t_quiz_result r on r.quiz_id=qf.id "
                    . "left join t_course_info cf on cf.id=qf.t_course_id "
                    . "where IF(r.STATUS='quizResultStatus.backed',r.back_result_end_time is null or r.back_result_end_time='' or SUBSTR(r.back_result_end_time,0,16)>NOW(),qf.result_end_time IS NULL OR qf.result_end_time ='' OR SUBSTR(qf.result_end_time,0,16)>NOW()) AND t_course_id in(select course_id from t_course_member where user_id=:create_by and del_flag=0) and qf.t_course_id is not null and qf.pid is null "
                    . "and qf.quiz_type = :quiz_type  and qf.quiz_status='activityStatus.ongoing' and (r.status is null or r.status='' or r.status='quizResultStatus.answerIn')  and qf.del_flag=0 and r.del_flag=0 and r.create_by=:create_by and cf.del_flag=0 $where order by r.create_time desc "." LIMIT ".$limit.",".$pageSize;
                $info= \Yii::$app->db->createCommand($quiz_sql)->bindValues($param)->queryAll();
            } else {
                //实训任务
                if(!empty($keyword)){
                    $param = array_merge($param,[':keyword'=>"%" . $keyword . "%"]);
                    $where = " and tct.task_title like :keyword ";
                }
                $sql="SELECT count(tctr.id)
                FROM
                        t_course_task_report tctr
                LEFT JOIN t_course_task tct ON tct.id = tctr.task_id
                LEFT JOIN t_course_info i ON tct.t_course_id = i.id
                LEFT JOIN t_course_schedule s ON s.id=tctr.schedule_id
                LEFT JOIN t_base_user_profile p ON p.user_id=tct.publish_by
                WHERE
                        tctr.create_by =:create_by
                AND tct.task_status IN (
                        'activityStatus.ongoing',
                        'activityStatus.over'
                ) and tctr.report_status='reportStatus.notComplete' 
                AND tct.del_flag = 0
                AND tctr.del_flag = 0
                AND i.del_flag=0  $where ";
                $sum = Yii::$app->db->createCommand($sql)->bindValues($param)->queryScalar();
                $sql="SELECT tctr.id resultId,tct.id quizId,tct.task_title quiz_name,tct.begin_time,tctr.report_status quizStatus,
                tct.deadline begin_time,
                tct.start_time result_end_time,
                i.name AS courseName,tct.t_course_id course_id
                FROM
                        t_course_task_report tctr
                LEFT JOIN t_course_task tct ON tct.id = tctr.task_id
                LEFT JOIN t_course_info i ON tct.t_course_id = i.id
                LEFT JOIN t_course_schedule s ON s.id=tctr.schedule_id
                LEFT JOIN t_base_user_profile p ON p.user_id=tct.publish_by
                WHERE
                        tctr.create_by =:create_by
                AND tct.task_status IN (
                        'activityStatus.ongoing',
                        'activityStatus.over'
                ) and tctr.report_status='reportStatus.notComplete' 
                AND tct.del_flag = 0
                AND tctr.del_flag = 0
                AND i.del_flag=0 $where  order by tct.publish_time desc "." LIMIT ".$limit.",".$pageSize;;
                $info = Yii::$app->db->createCommand($sql)->bindValues($param)->queryAll();
            }
            $result=[
                'msg'=>'查询成功',
                "ver"=> "1.5.0",
//                'data'=>$resultInfo,
                "data"=>[
                    "totalCount"=>(int)$sum,
                    "activeList"=>$info
                ],
                'code'=>'200'
            ];
            Yii::$app->end(json_encode($result));
        }        
    }
}
